package com.tianqu.tms.www.car.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.Car;

/**
 * 展售单打印 - action
 * @author mufeiyu
 *
 */
public class CarSearchSoldOurInfoPrintAjaxAction extends TmsAjaxAction {
	private Logger logger = Logger.getLogger(CarSearchSoldOurInfoPrintAjaxAction.class);

	@Override
	public void process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");

		//车辆编号
		String serial = request.getParameter("serial");
		//车辆名称
		String carName = request.getParameter("carName");
		//配置说明
		String remark = request.getParameter("remark");
		try {
			if(!StringUtil.isNullOrBlank(carName)){
				carName = URLDecoder.decode(carName,"utf-8");
			}else {
				carName = "";
			}
			if(!StringUtil.isNullOrBlank(remark)){
				remark = URLDecoder.decode(remark,"utf-8");
			}else{
				remark = "";
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//上牌
		String regDate = request.getParameter("regDate");
		//表显里程
		String mileage = request.getParameter("mileage");
		//联系电话
		String companyContactPhone = request.getParameter("companyContactPhone");

		if(!StringUtil.isNullOrBlank(regDate)){
			String year = regDate.substring(0, 4);
			String month = regDate.substring(5, 7);
			regDate = year + "年" + month + "月";
		}
		mileage = StringUtil.removeDH(mileage);
		int mileageValue = 0;
		if(!StringUtil.isNullOrBlank(mileage)){
			mileageValue = Integer.valueOf(mileage);
			mileage = StringUtil.transYtoW(mileageValue) + "万公里";	
			mileage.replaceAll(".", "▪");
		}
		Car car = new Car();
		car = car.findCarBySerial(serial);
		
		String background = "";
		 if(car.getAttestStatus() == 2){
			background = "<img class='jp-paper-background' src='style/images/menuFX.jpg'  style='WIDTH:1586px; HEIGHT:1122px'/>";			
		}else{		
			background = "<img class='jp-paper-background' src='style/images/menuZS.jpg' style='WIDTH:1586px; HEIGHT:1122px'/>";
		}
		// 拼select
		StringBuffer str = new StringBuffer();
		str.append(""
						+ ""+background+""
						+ "<div class='jp-label jp-comp-801'>" + carName + "</div>"
						+ "<div class='jp-label jp-comp-803'>" + regDate + "</div>"
						+ "<div class='jp-label jp-comp-804'>" + mileage + "</div>"
						+ "<div class='jp-label jp-comp-805'>" + companyContactPhone + "</div>"						
						+ "<div id=\"ipt\" class='jp-label jp-comp-812'>"+ remark +"</div>");

		String responseText1 = str.toString();
		str.append("##");
		str.append("1");
		str.append("##");
		str.append(serial);
		
		String responseText = str.toString();
		
		uc.setAttribute("printText", responseText1);
		uc.setAttribute("soldOurPrintFlag", "1");
		uc.setAttribute("innerSoldFlag", "1");

		responseValues(response, responseText, TEXT);
		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
}